package com.eaglefly.redis.scan;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import static org.springframework.util.Assert.notNull;

/**
 * 功能：redisDao 自动注册类
 * 时间：2018/1/5 15:19
 *
 * @author zhaojinpeng
 */
public class RedisDaoScannerConfigurer implements BeanDefinitionRegistryPostProcessor, InitializingBean,
        ApplicationContextAware, BeanNameAware {

    private final static Logger logger = LoggerFactory.getLogger(RedisDaoScannerConfigurer.class);

    /**
     * beanName
     */
    private String beanName;
    /**
     * scanPackage 扫描包位置
     */
    private String[] scanPackages;
    /**
     * spring 上下文
     */
    private ApplicationContext applicationContext;

    @Override
    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        notNull(this.scanPackages, "Property 'scanPackages' is required");
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        //创建
        ClassPathDaoScanner scanner = new ClassPathDaoScanner(beanDefinitionRegistry);
        //执行
        scanner.registerFilters();
        scanner.scan(scanPackages);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

    public String[] getScanPackages() {
        return scanPackages;
    }

    public void setScanPackages(String[] scanPackages) {
        this.scanPackages = scanPackages;
    }
    
}
